//
// Created by yanhai on 2021/11/4.
//

#include <cstdlib>
#include <cstring>

#include "packet.h"
#include "flow.h"

static Flow *FlowAlloc()
{
    Flow *f;
    size_t size = sizeof(Flow);

    f = static_cast<Flow *>(malloc(size));
    if (f == nullptr) {
        return nullptr;
    }
    memset(f, 0, size);

    return f;
}

Flow *FlowGetNew()
{
    Flow *f = FlowAlloc();
    if (f != nullptr) {
        f->mutex.lock();
    }
    return f;
}

/* initialize the flow from the first packet
 * we see from it. */
void FlowInit(Flow *f, const Packet *p)
{
    f->src_addr = p->src_addr;
    f->dst_addr = p->dst_addr;
    f->src_port = p->src_port;
    f->dst_port = p->dst_port;
}

bool Flow::operator==(const Packet &p) const
{
    if (src_addr != p.src_addr)
        return false;
    if (dst_addr != p.dst_addr)
        return false;
    if (src_port != p.src_port)
        return false;
    if (dst_port != p.dst_port)
        return false;

    return true;
}
